<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="流程名称" name="flowTitle">
						<a-input v-model:value="searchFormState.flowTitle" placeholder="请输入流程标题" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="流程类型" name="flowType">
						<a-select
							v-model:value="searchFormState.flowType"
							placeholder="请选择流程类型"
							:options="flowTypeOptions"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
			:loading="loading"
		>
			<template #operator class="table-operator">
				<a-space>
					<xn-batch-delete :selectedRowKeys="selectedRowKeys" @batchDelete="deleteBatchSysFlowDraft" />
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'flowType'">
					{{ $TOOL.dictTypeData('FLOW_TYPE', record.flowType) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="flowDetail(record)">详情</a>
						<a-divider type="vertical" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteSysFlowDraft(record)">
							<a-button type="link" danger size="small">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<dtForm ref="dtFromRef" :isPreview="false" @successful="table.refresh(true)" />
	<preForm ref="preFormRef" />
</template>

<script setup name="flowdraft">
	import tool from '@/utils/tool'
	import dtForm from '@/views/flow/dtform/index.vue'
	import preForm from '@/views/flow/dtform/pre_form.vue'
	import sysFlowDraftApi from '@/api/flow/sysFlowDraftApi'
	let searchFormState = reactive({})
	const searchFormRef = ref()
	const loading = ref(false)
	const table = ref()
	const formRef = ref()
	const dtFromRef = ref()
	const { proxy } = getCurrentInstance()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '流程名称',
			dataIndex: 'flowTitle'
		},
		{
			title: '流程类型',
			dataIndex: 'flowType'
		},
		{
			title: '申请人',
			dataIndex: 'createUser'
		},
		{
			title: '申请时间',
			dataIndex: 'createTime'
		}
	]
	// 操作栏通过权限判断是否显示
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: '150px'
	})
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return sysFlowDraftApi.sysFlowDraftPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
	/**
	 * 发起申请
	 */
	const flowDetail = (r) => {
		r.formTitle = r.flowTitle
		if (r.formType == '1') {
			proxy.$refs.preFormRef.onOpen(r)
		} else {
			r.formRouter = r.formJson
			dtFromRef.value.onOpen(r)
		}
	}
	// 删除
	const deleteSysFlowDraft = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		sysFlowDraftApi.sysFlowDraftDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchSysFlowDraft = (params) => {
		sysFlowDraftApi.sysFlowDraftDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
	const flowTypeOptions = tool.dictList('FLOW_TYPE')
</script>
